#include <stdio.h>
#include <stack>

struct ListNode
{
    int m_nKey;
    ListNode* m_pNext;
};
// 递归方式
void PrintListReversingly_Recursively(ListNode* pHead)
{
    if(pHead == NULL) return;

    if(pHead->m_pNext != NULL)
    {
        PrintListReversingly_Recursively(pHead->m_pNext);
    }
    printf("%d\t", pHead->m_nKey);
}

// 循环加stack的方式
void PrintListReversingly_Loop(ListNode* pHead)
{
    if(pHead == NULL) return;

    std::stack<ListNode*> nodes;
    ListNode* curr = pHead;
    while(curr != NULL)
    {
        nodes.push(curr);
        curr = curr->m_pNext;
    }

    while(!nodes.empty())
    {
        ListNode* t = nodes.top();
        nodes.pop();
        printf("%d\t", t->m_nKey);
    }
    
}

int main()
{
    // 正常测试
    ListNode head = {10, NULL};
    PrintListReversingly_Recursively(&head);
    PrintListReversingly_Loop(&head);

    ListNode node = {12, NULL};
    head.m_pNext = &node;
    PrintListReversingly_Recursively(&head);
    PrintListReversingly_Loop(&head);

    // 鲁棒性测试
    ListNode* p = NULL;
    PrintListReversingly_Recursively(p);
    PrintListReversingly_Loop(p);

    return 0;
}